package com.koron.css2.baseConfig.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.CostInvoiceBean;
import com.koron.css2.baseConfig.mapper.CostInvoiceMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.CommonUtils;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bson.types.ObjectId;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;
/**
 * 短信模板-添加
 * @author 许海宜
 */
public class CostInvoiceAdd implements ServerInterface {

	private static Logger logger = LogManager.getLogger(CostInvoiceAdd.class);

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		try {
			CostInvoiceMapper mapper = factory.getMapper(CostInvoiceMapper.class);
			//清空
			mapper.deleteAll();
			//插入新记录
			List<CostInvoiceBean> beans = JsonUtils.jsonToList(JsonUtils.objectToJson(req.getData()), CostInvoiceBean.class);
			if(beans == null || beans.size() == 0) {
				factory.close();
				return MessageBean.create(Constant.NOT_NULL, "参数为空", void.class);
			}
			for (CostInvoiceBean bean : beans) {
				bean.setId(new ObjectId().toHexString());
				bean.setStatus("1");
				bean.setCreateName(userInfo.getUserInfo().getName());
				bean.setCreateAccount(userInfo.getUserInfo().getAcount());
				bean.setCreateTime(CommonUtils.getCurrentTime());
				mapper.insert(bean);
			}
		} catch (Exception e) {
			factory.close();
			logger.error("操作失败", e);
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "操作失败", void.class);
		}
		return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", void.class);
	}
}